
package com.yimixian.app.store;

import com.yimixian.app.R;
import com.yimixian.app.model.Store;
import com.yimixian.app.util.SharedPreferencesHelper;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;

import butterknife.ButterKnife;
import butterknife.InjectView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class StoresView extends FrameLayout {

    public interface Listener {
        void onStoreClicked(int storeId);

        void onStoreInfoIconClicked(Store store);
    }

    @InjectView(R.id.section_list)
    ListView mSectionList;
    @InjectView(R.id.store_list)
    ListView mStoreList;
    @InjectView(R.id.divider_view)
    View mDividerView;

    private ArrayList<String> mSections = new ArrayList<>();
    private ArrayList<Store> mShowStores = new ArrayList<>();
    private String mSelectedSectionName;
    private Store mSelectedStore;
    private OnClickListener mClickListener;
    private BaseAdapter mStoreAdapter;
    private HashMap<String, ArrayList<Store>> mAreaToStoreMap;
    private BaseAdapter mSectionAdapter;
    private Listener mListener;

    public StoresView(Context context) {
        super(context);
        initStoresView();
    }

    public StoresView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initStoresView();
    }

    public StoresView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initStoresView();
    }

    private void initStoresView() {
        LayoutInflater.from(getContext()).inflate(R.layout.stores_view, this);
        ButterKnife.inject(this);
        FrameLayout.LayoutParams dividerViewParams = new FrameLayout.LayoutParams(
                1, LayoutParams.MATCH_PARENT);
        dividerViewParams.gravity = Gravity.RIGHT;
        mDividerView.setLayoutParams(dividerViewParams);
        mClickListener = new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (view instanceof SectionAreaItemView) {
                    mSelectedSectionName = ((SectionAreaItemView) view).getSectionName();
                    mShowStores = mAreaToStoreMap.get(mSelectedSectionName);
                    mStoreAdapter.notifyDataSetChanged();
                    mSectionAdapter.notifyDataSetChanged();
                } else if (view instanceof StoreItemView) {
                    if (mListener != null) {
                        mListener.onStoreClicked(((StoreItemView) view).getStoreId());
                        SharedPreferencesHelper
                                .setPickUpStoreName(((StoreItemView) view).mStore.name);
                    }
                }
            }
        };
        mSectionAdapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return mSections.size();
            }

            @Override
            public String getItem(int position) {
                return mSections.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = new SectionAreaItemView(getContext());
                    convertView.setOnClickListener(mClickListener);
                }
                SectionAreaItemView itemView = (SectionAreaItemView) convertView;
                String sectionName = getItem(position);
                itemView.init(sectionName);
                itemView.setClicked(sectionName.equals(mSelectedSectionName));
                return itemView;
            }
        };
        mSectionList.setAdapter(mSectionAdapter);
        mStoreAdapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return mShowStores.size();
            }

            @Override
            public Store getItem(int position) {
                return mShowStores.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = new StoreItemView(getContext());
                    convertView.setOnClickListener(mClickListener);
                }
                StoreItemView itemView = (StoreItemView) convertView;
                Store store = getItem(position);
                itemView.init(store);
                if (mListener != null) {
                    itemView.setOnInfoIconClickListener(mListener);
                }
                itemView.setClicked(store.equals(mSelectedStore));
                return itemView;
            }
        };

        mStoreList.setAdapter(mStoreAdapter);
    }

    public void bind(List<Store> stores, int storeId) {
        mSections = new ArrayList<String>();
        mAreaToStoreMap = new HashMap<>();
        mSelectedSectionName = "";
        for (Store store : stores) {
            if (storeId == store.id) {
                mSelectedSectionName = store.area;
                mSelectedStore = store;
            }

            if (!mSections.contains(store.area)) {
                mSections.add(store.area);
            }
            if (mAreaToStoreMap.get(store.area) == null) {
                ArrayList<Store> storeList = new ArrayList<>();
                mAreaToStoreMap.put(store.area, storeList);
            }
            mAreaToStoreMap.get(store.area).add(store);
        }
        if (mSections.size() > 0) {
            if (TextUtils.isEmpty(mSelectedSectionName)) {
                mSelectedSectionName = mSections.get(0);
            }
            mShowStores = mAreaToStoreMap.get(mSelectedSectionName);
        }
        mSectionAdapter.notifyDataSetChanged();
        mStoreAdapter.notifyDataSetChanged();

    }

    public void setListener(Listener listener) {
        mListener = listener;
    }

}
